from pygame import *
from pygame.locals import *
from random import uniform
import math
from bullet import *


# This class contains everything that is needed to create an enemy.
# 
# 
class Enemy:
     def __init__(self, posx, posy, speed, enemyType, ID):
          self.posx = posx
          self.posy = posy
          
          if enemyType == 1:
               self.width = 32
               self.height = 52
               
          self.targetx = posx
          self.targety = posy
          self.overloadx = 0
          self.overloady = 0
          self.factor = 0
          self.speed = speed
          self.enemyID = ID
          self.enemyType = enemyType
          self.bulletArray = []
          
          self.life = 100
          self.alive = True
          self.moving = False

          
     def setTarget(self, targetx, targety):
          self.targetx = targetx
          self.targety = targety
     
     def move(self):
          if not(self.targetx == self.posx and self.targety == self.posy):
               dirx = self.targetx - self.posx
               diry = self.targety - self.posy
               length = math.sqrt(dirx * dirx + diry * diry)
               if(length > self.speed):
                    dirx = dirx * self.speed / length
                    diry = diry * self.speed / length
                    self.posx = self.posx + int(dirx)
                    self.posy = self.posy + int(diry)
                    if(self.overloadx > 1):
                         self.posx += 1
                         self.overloadx -= 1               
                    if(self.overloady > 1):
                         self.posy += 1
                         self.overloady -= 1               
                    if(self.overloadx < -1):
                         self.posx -= 1                    
                         self.overloadx += 1                    
                    if(self.overloady < -1):
                         self.posy -= 1
                         self.overloady += 1                    
                    self.overloadx += dirx - int(dirx)
                    self.overloady += diry - int(diry)
     
     def fire(self, bulletGoalx, bulletGoaly):

          self.bulletArray.append(Bullet(self.posx, self.posy, self.posx, self.posy - 1000))
               
          if self.weaponCooldown > 0:
               self.weaponCooldown -= 1
     
     def deal_dmg(self, amount):
          self.life -= amount
     
